>>>cat~/programing/mdns_scanner.pyfromzeroconfimportZeroconf,ServiceBrowser,ServiceListenerimportthreadingimporttimediscovered_types=set()classServiceInstanceListener(ServiceListener):defadd_service(self,zeroconf,type,name):info=zeroconf.get_service_info(type,name)ifinfo:print(f"[+] Instance Found: {name}")print(f" 🔹 Server: {info.server}")print(f" 🔹 Port: {info.port}")print(f" 🔹 Addresses: {info.parsed_addresses()}")print(f" 🔹 Weight: {info.weight}")print(f" 🔹 Priority: {info.priority}")print(f" 🔹 Properties:")forkey,valueininfo.properties.items():try:print(f" - {key.decode('utf-8')}: {value.decode('utf-8')}")except:print(f" - {key}: {value}")else:print(f" [!] No info available for {name}")defupdate_service(self,zeroconf,type,name):# Optional: print updates if you want
print(f"[~] Service updated: {name}")defremove_service(self,zeroconf,type,name):print(f"[-] Service removed: {name}")classTypeDiscoveryListener(ServiceListener):defadd_service(self,zeroconf,type,name):# Only handle new types
ifname.endswith(".local.")andnamenotindiscovered_types:discovered_types.add(name)print(f"[🔎] Discovered Service Type: {name}")# Start browser for each service type
ServiceBrowser(zeroconf,name,ServiceInstanceListener())defmain():zeroconf=Zeroconf()print("[*] Scanning for mDNS services on your local network...")ServiceBrowser(zeroconf,"_services._dns-sd._udp.local.",TypeDiscoveryListener())try:whileTrue:time.sleep(0.5)exceptKeyboardInterrupt:print("[!] Stopping scan.")zeroconf.close()if__name__=="__main__":main()